<template>
  <div class="login-container">
    <div class="login-box">
      <!--      登录框图片区域     -->
      <div class="avatar-box">
        <img src="../../assets/logo.png" alt="登录框图片">
      </div>

      <!--      登录框表单区域     -->
      <el-form label-width="60px" class="login-form" ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
        <!--   用户名区域     -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>

        <!--   密码区域     -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password"  @keyup.enter="login"></el-input>
        </el-form-item>

        <!--   按钮区域     -->
        <el-row justify="end">
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, reactive} from "vue";
import {Login} from "@/api";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
const router = useRouter()
// 登陆表单绑定的数据对象
const loginForm = reactive({
  username: 'admin',
  password: '123456'
})
// 登陆表单绑定的数据验证规则
const loginFormRules = reactive({
  // 验证用户名是否合法
  username: [
    {required: true, message: '请输入登陆用户名', trigger: 'blur'},
    {min: 3, max: 10, message: '长度在3到10个字符', trigger: 'blur'}
  ],
  // 验证密码是否合法
  password: [
    {required: true, message: '请输入登陆密码', trigger: 'blur'},
    {min:6, max: 10, message: '长度在6到15个字符', trigger: 'blur'}
  ]
})
// 重置整个表单
const loginFormRef: any = ref({})
const resetLoginForm = ()=>{
  loginFormRef.value.resetFields()
}
// 登陆验证
const login = ()=>{
  loginFormRef.value.validate(async (valid: boolean)=>{
    if (!valid) {return}
    const {data: response} = await Login(loginForm)
    if (response.meta.status !== 200) {return ElMessage.error('登陆失败')}
    ElMessage.success('登陆成功')
    window.sessionStorage.setItem('token', response.data.token)
    await router.push('/home')
  })
}
</script>

<style lang="less" scoped>
.login-container{
  background-color: #102d4f;
  height: 100%;
  .login-box{
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    .avatar-box{
      width: 130px;
      height: 130px;
      border: 1px solid #cccccc;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      background-color: #ffffff;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #cccccc;
      }
    }

    .login-form{
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }

  }


}
</style>

